//
//  ViewController.m
//  视图位置任意调整
//
//  Created by Hello Cai on 2019/7/3.
//  Copyright © 2019 Hello Cai. All rights reserved.
//
#define SCREEN_W [UIScreen mainScreen].bounds.size.width

#import "CodeViewController.h"
#import "UIView+GHFrame.h"

@interface CodeViewController ()
@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)UIView *v1;
@property(nonatomic, strong)UIView *v2;
@property(nonatomic, strong)UIView *v3;
@property(nonatomic, strong)UIView *v4;
@end

@implementation CodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *headerView = [[UIView alloc] init];
    self.headerView = headerView;
    headerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:headerView];
    
    UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 60)];
    v1.backgroundColor = [UIColor blueColor];
    [headerView addSubview:v1];
    self.v1 = v1;
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab.text = @"视图1";
    [v1 addSubview:lab];
    
    
    UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(v1.frame), SCREEN_W, 90)];
    [headerView addSubview:v2];
    self.v2 = v2;
    v2.backgroundColor = [UIColor greenColor];
    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab2.text = @"视图2";
    [v2 addSubview:lab2];
    
    
    UIView *v3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(v2.frame), SCREEN_W, 120)];
    [headerView addSubview:v3];
    self.v3 = v3;
    v3.backgroundColor = [UIColor yellowColor];
    UILabel *lab3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab3.text = @"视图3";
    [v3 addSubview:lab3];
    
    
    UIView *v4 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(v3.frame), SCREEN_W, 160)];
    [headerView addSubview:v4];
    self.v4 = v4;
    v4.backgroundColor = [UIColor purpleColor];
    UILabel *lab4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab4.text = @"视图4";
    [v4 addSubview:lab4];
    
    headerView.width = SCREEN_W;
    headerView.height = v1.height+v2.height+v3.height+v4.height;
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 500, 60, 40)];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
}

-(void)btnClick:(UIButton *)button{
    button.selected = !button.selected;
    _v1.hidden = NO;
    _v2.hidden = NO;
    _v3.hidden = NO;
    _v4.hidden = NO;
    
    if (button.selected==YES) {
        //1，3，4
        _v2.hidden = YES;
        _v1.y = 0;
        _v3.y = CGRectGetMaxY(_v1.frame);
        _v4.y = CGRectGetMaxY(_v3.frame);
    }else{
        //4，3，2，1
        _v4.y = 0;
        _v3.y = CGRectGetMaxY(_v4.frame);
        _v2.y = CGRectGetMaxY(_v3.frame);
        _v1.y = CGRectGetMaxY(_v2.frame);
    }
    _headerView.height = (_v1.hidden?0:_v1.height) + (_v2.hidden?0:_v2.height) + (_v3.hidden?0:_v3.height) + (_v4.hidden?0:_v4.height);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


